package com.zdp.provider.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zdp.provider.service.CourseService;
import com.zdp.provider.service.DemoController;
import com.zdp.provider.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 类描述: 教师信息服务
 *
 * @author ZhhengDP
 * @since 2021/8/16
 */
@RestController
@RequestMapping("/teacher_info")
public class TeacherController {


    private TeacherService teacherService;

    /**
     * 这边的降级也是可以生效的
     */
    private CourseService courseService;

    /**
     * 这边的降级也是可以生效的
     */
    private DemoController demoController;


    private RestTemplate restTemplate;

    @Autowired
    public TeacherController(CourseService courseService,
                             TeacherService teacherService,
                             DemoController demoController,
                             RestTemplate restTemplate
                             ) {
        this.courseService = courseService;
        this.teacherService = teacherService;
        this.demoController = demoController;
        this.restTemplate = restTemplate;
    }

    @GetMapping("/teacher")
    public TeacherVo getTeacher(Integer teacherId){
        TeacherVo teacher = teacherService.getTeacherById(teacherId);
        List<CourseDto> courses = courseService.loadCourses(teacherId);
        teacher.setCourses(courses);
        return teacher;
    }



    /**
     * @HystrixCommand 用于指定哪个方法需要进行容错保护
     * 属性 fallbackMethod 用于指定当方法发生异常时，用哪个方法来进行服务降级处理（方法替换）
     * 这边的降级不起作用，不知道为什么？
     * 但如果把注解加在 getTeacher 方法上，降级就可以生效了。好奇怪，到时候研究一下
     * */
    @HystrixCommand(fallbackMethod = "loadCoursesFallBack")
    public List<CourseDto> loadCourses(Integer teacherId){
        // 使用服务名来进行服务的调用
        String url = "http://127.0.0.1:9907/course_info/courses?teacherId={teacherId}";
        HashMap<String,Object> param = new HashMap<>();
        param.put("teacherId",teacherId);
        List<CourseDto> result = restTemplate.getForObject(url, List.class, param);
        return result;

    }

    protected List<CourseDto> loadCoursesFallBack(Integer teacherId){
        List<CourseDto> result = new ArrayList<>();
        CourseDto course1 = new CourseDto();
        CourseDto course2 = new CourseDto();
        course1.setContent("容错课程介绍1");
        course1.setName("容错课程1");
        course1.setTeacherId(teacherId);
        course1.setCourseServicePort(9906);
        /**====================================*/
        course2.setContent("容错课程介绍2");
        course2.setName("容错课程2");
        course2.setTeacherId(teacherId);
        course2.setCourseServicePort(9906);

        result.add(course1);
        result.add(course2);
        return result;
    }



}
